$(function() {
    var topicTable = null;
    var opts = {
        targetId: "topicTable",
        templateId: "tabletopic",
        settings: {
            url: DI.queryCommList,
            ajaxType: "json",
            sourceInterface: {
                INum: "total",
                IData: "rows"
            },
            paginationIndexName: {
                "peerpageRows": "pageSize",
                "currentPage": "pageNo"
            }
        },
        customFuncs: {
            delPop: function(commid) {
                popCtrl.confirmType = "del";
                popCtrl.delcommId = commid;
                $("#popup_confirm").show();
            },
            toBest: function(comm){
                popCtrl.positionName = comm.position;
                popCtrl.bestCommObj = comm;
                $("#popup_cnt").show();
            }
        }

    };
    topicTable = SwiftUI.swiftTable.getInstance(opts);
    //主区域
    var mainCtrl = avalon.define({
        $id: "mainController",
        topicKey: "",
        cntKey: "",
        //新增标签
        add: function() {
            $("#popup_cnt").show();
        },
        searchData: function(){
            var searchParam = {
                "pageNo": 1,
                "pageSize": 10,
                "topicName": mainCtrl.topicKey,
                "commContent": mainCtrl.cntKey
            };
            topicTable.search(searchParam);
        }
    });
    //弹框区域
    var popCtrl = avalon.define({
        $id: "popController",
        deltag: "",
        confirmType: "del",
        topicName: "",
        endTime: "",
        supViewTip: "",
        rejViewTip: "",
        imgUrl: "", //上传后的图片url
        positionName: "",
        bestCommObj: {},
        delcommId: {},
        
        del: function() {
            $.ajax({
                type: "post",
                dataType: "json",
                data: {
                    "commId": popCtrl.delcommId
                },
                url: DI.delComm,
                success: function(data) {
                    $("#popup_confirm").hide();
                    if(data.retFlag == 1) {
                        PLATFORM.showTips("success", "删除成功");
                        var searchParam = {
                            "pageNo": 1,
                            "pageSize": 10
                        };
                        topicTable.search(searchParam);
                    } else {
                        PLATFORM.showTips("error", data.errorMessage);
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });
        },
        save: function(){
            $.ajax({
                type: "post",
                dataType: "json",
                data: {
                    "commId": popCtrl.bestCommObj.commId,
                    "isBest": !popCtrl.bestCommObj.isBest,
                    "personId": popCtrl.bestCommObj.personId,
                    "position": popCtrl.positionName,
                },
                url: DI.toBeBest,
                success: function(data) {
                    $("#popup_cnt").hide();
                    if(data.retFlag == 1) {
                        PLATFORM.showTips("success", "保存成功");
                        var searchParam = {
                            "pageNo": 1,
                            "pageSize": 10,
                            "topicName": "",
                            "commContent": ""
                        };
                        topicTable.search(searchParam);
                    } else {
                        PLATFORM.showTips("error", data.errorMessage);
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });
        }
    });

    avalon.scan();
});
